package com.ctgu.edu.controller;

import com.ctgu.edu.pojo.*;
import com.ctgu.edu.service.impl.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.LinkedList;

@Controller
public class SeekerController {

    @Autowired
    @Qualifier("SeekerServiceImpl")
    private SeekerServiceImpl seekerServiceImpl;
    @Autowired
    @Qualifier("jobServiceImpl")
    private JobServiceImpl jobServiceImpl;
    @Autowired
    @Qualifier("resumeServiceImpl")
    private ResumeServiceImpl resumeServiceImpl;
    @Autowired
    @Qualifier("applyServiceImpl")
    private ApplyServiceImpl applyServiceImpl;

    //显示主页
    //处理多个路径
    @RequestMapping("/seeker/mainpage")
    public String RecruiterMainpage(Model model, HttpServletRequest request) {
        Integer startIndex = 0;
        Integer pageSize = 10;
        HttpSession session = request.getSession();
        Seeker seeker =(Seeker) session.getAttribute("seeker"); //获取当前登录的用户
        //添加个人信息
        model.addAttribute("seeker",seeker);
        //添加工作信息
        LinkedList<Job> jobs = jobServiceImpl.getJobByClickTimes(startIndex,pageSize);
        model.addAttribute("jobs",jobs);

        /*for (Job job : jobs) {
            System.out.println(job.toString());
        }*/
        Boolean search = false;
        model.addAttribute("search",search);
        return "seeker_main_page";
    }

    @RequestMapping("/seeker/mainpage/{startIndex}/{pageSize}")
    public String RecruiterMainpageIndex(@PathVariable("startIndex") Integer startIndex,@PathVariable("pageSize" ) Integer pageSize, Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        Seeker seeker =(Seeker) session.getAttribute("seeker"); //获取当前登录的用户
        //添加个人信息
        model.addAttribute("seeker",seeker);
        //添加工作信息
        LinkedList<Job> jobs = new LinkedList<Job>();
        jobs = jobServiceImpl.getJobByClickTimes(startIndex,pageSize);
        model.addAttribute("jobs",jobs);

        Integer jobNum = jobServiceImpl.getJobNum();
        Integer pageNum;
        if(jobNum%pageSize == 0){
            pageNum = jobNum/pageSize;
        }else{
            pageNum = jobNum/pageSize + 1;
        }

        model.addAttribute("pageNum",pageNum);

        Boolean sohowPage = true;
        model.addAttribute("sohowPage",sohowPage);
        return "seeker_main_page";
    }

    @RequestMapping("/seeker/resume/edit")
    String EditResume(Resume resume,Model model, HttpServletRequest request){
        HttpSession session = request.getSession();
        Seeker seeker =(Seeker) session.getAttribute("seeker"); //获取当前登录的用户
        resume.setResumeOwnerId(seeker.getSeekerId());

        Resume existResume;
        existResume = resumeServiceImpl.getResumeByOwnerId(seeker.getSeekerId());
        //已存在，更新
        if(existResume != null){
            resume.setResumeId(existResume.getResumeId());
            resumeServiceImpl.updateResume(resume);
        }else{
            //不存在，添加！
            resume.setResumeOwnerId(seeker.getSeekerId());
            resumeServiceImpl.addResume(resume);
        }
        return "forward:/seeker/mainpage/0/10";
    }

    @RequestMapping("/seeker/apply")
    public String LookJobApply(Model model, HttpServletRequest request){
        HttpSession session = request.getSession();
        Seeker seeker =(Seeker) session.getAttribute("seeker"); //获取当前登录的用户

        LinkedList<Apply> applys = applyServiceImpl.getApplyBySeekerId(seeker.getSeekerId());
        LinkedList<ApplyJobInf> applyJobInfs = new LinkedList<ApplyJobInf>();
        Job tempjob;
        for (Apply apply : applys) {
            tempjob = jobServiceImpl.getJobById(apply.getApplyJobId());
            ApplyJobInf applyJobInf = new ApplyJobInf(apply,tempjob);
            applyJobInfs.add(applyJobInf);
        }

        model.addAttribute("applyJobInfs",applyJobInfs);
        return "seeker_apply";
    }

    @RequestMapping("/seeker/apply/delete/{applyJobId}")
    public String DeleteApply(@PathVariable("applyJobId") Integer applyJobId,Model model, HttpServletRequest request){
        HttpSession session = request.getSession();
        Seeker seeker =(Seeker) session.getAttribute("seeker"); //获取当前登录的用户

        applyServiceImpl.deleteApplyById(seeker.getSeekerId(),applyJobId);
        model.addAttribute("inf_seeker_apply","删除成功！！");
        return "forward:/seeker/apply";
    }


    @RequestMapping("/seeker/sefinf/edit")
    public String EditSelfInf(Seeker editedSeeker,Model model, HttpServletRequest request){
        HttpSession session = request.getSession();
        Seeker seeker =(Seeker) session.getAttribute("seeker"); //获取当前登录的用户

        editedSeeker.setSeekerId(seeker.getSeekerId());

        seekerServiceImpl.updateSeeker(editedSeeker);
        session.setAttribute("seeker",editedSeeker);
        model.addAttribute("inf_selfinf_edit","修改个人信息成功");
        return "forward:/seeker/mainpage/0/10";
    }

    @RequestMapping("/seeker/sefinf/resume")
    public String EditSelfInf(Model model, HttpServletRequest request){
        HttpSession session = request.getSession();
        Seeker seeker =(Seeker) session.getAttribute("seeker"); //获取当前登录的用户
        seeker = seekerServiceImpl.getSeekerById(seeker.getSeekerId());

        Resume resume;
        resume = resumeServiceImpl.getResumeByOwnerId(seeker.getSeekerId());

        model.addAttribute("resume",resume);
        model.addAttribute("seeker",seeker);

        return "seeker_resume";
    }

    @RequestMapping("/seeker/job/search")
    public String EditSelfInf(String jobKey,Model model,HttpServletRequest request){
        HttpSession session = request.getSession();
        Seeker seeker =(Seeker) session.getAttribute("seeker"); //获取当前登录的用户
        LinkedList<Job> jobs = jobServiceImpl.getJobLike(jobKey);
        model.addAttribute("jobs",jobs);

        Integer pageNum = 0;
        model.addAttribute("pageNum",pageNum);
        Boolean sohowPage = false;
        model.addAttribute("sohowPage",sohowPage);
        return "seeker_main_page";
    }
}
